package de.slothsoft.wasbinich.android.preference;

import android.app.Activity;
import android.content.Context;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import de.slothsoft.wasbinich.android.util.MediaPlayerUtil;

public final class PreferenceUtil {

    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */

    private static final Preference.OnPreferenceChangeListener PREFERENCE_TO_VALUE_LISTENER = new Preference.OnPreferenceChangeListener() {
	@Override
	public boolean onPreferenceChange(Preference preference, Object value) {
	    String stringValue = value.toString();

	    if (preference instanceof ListPreference) {
		// For list preferences, look up the correct display value in
		// the preference's 'entries' list.
		ListPreference listPreference = (ListPreference) preference;
		int index = listPreference.findIndexOfValue(stringValue);

		// Set the summary to reflect the new value.
		preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);

	    } else {
		// For all other preferences, set the summary to the value's
		// simple string representation.
		preference.setSummary(stringValue);
	    }
	    return true;
	}
    };

    /**
     * Binds a preference's summary to its value. More specifically, when the
     * preference's value is changed, its summary (line of text below the
     * preference title) is updated to reflect the value. The summary is also
     * immediately updated upon calling this method. The exact display format is
     * dependent on the type of preference.
     * 
     * @see #sBindPreferenceSummaryToValueListener
     */

    public static void bindPreferenceSummaryToValue(Preference preference) {
	preference.setOnPreferenceChangeListener(PREFERENCE_TO_VALUE_LISTENER);
	PREFERENCE_TO_VALUE_LISTENER.onPreferenceChange(preference, getCurrentValue(preference, "")); //$NON-NLS-1$
    }

    public static void bindPreferenceToLocale(Activity settingsActivity, Preference localePreference) {
	LocalePreferenceChangeListener listener = new LocalePreferenceChangeListener(settingsActivity);
	localePreference.setOnPreferenceChangeListener(listener);
	listener.onPreferenceChange(localePreference, getCurrentValue(localePreference, "")); //$NON-NLS-1$
    }

    public static void bindPreferenceToMusic(Activity settingsActivity, final Preference musicPreference) {
	Preference.OnPreferenceChangeListener listener = new Preference.OnPreferenceChangeListener() {

	    @Override
	    public boolean onPreferenceChange(Preference preference, Object newValue) {
		MediaPlayerUtil.stopMusic();
		MediaPlayerUtil.userWantsMusic = ((Boolean) newValue).booleanValue();
		return true;
	    }

	};
	musicPreference.setOnPreferenceChangeListener(listener);
	listener.onPreferenceChange(musicPreference, getCurrentBoolean(musicPreference, true));
    }

    public static String getCurrentValue(Preference preference, String defaultValue) {
	return getCurrentValue(preference.getContext(), preference.getKey(), defaultValue);
    }

    public static String getCurrentValue(Context context, String key, String defaultValue) {
	return PreferenceManager.getDefaultSharedPreferences(context).getString(key, defaultValue);
    }

    public static boolean getCurrentBoolean(Preference preference, boolean defaultValue) {
	return getCurrentBoolean(preference.getContext(), preference.getKey(), defaultValue);
    }

    public static boolean getCurrentBoolean(Context context, String key, boolean defaultValue) {
	return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defaultValue);
    }

    private PreferenceUtil() {
	// hide me from the world
    }
}
